package org.compiere.model;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Trx;
import org.erps.MakeSwift;

public class CalloutBCSFRInputFormLine extends CalloutEngine {
	
	private static CLogger s_log = CLogger.getCLogger(MBCSFRInputForm.class);
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
//	public String calcColumnValue1(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) {
//		
//		int l = mTab.getRecord_ID();
//		String sql = "update BCS_FRInputFormLine set ColumnValue1 = " + value + " where  BCS_FRInputFormLine_ID = " + l;
//		DB.executeUpdate(sql);
//		
//		String result = "";
//		
//		Integer ID = (Integer) mTab.getValue("BCS_FRInputFormLine_ID");
//		
//		MBCSFRInputFormLine line = new MBCSFRInputFormLine(ctx, ID.intValue(), null);
//
//		List<MBCSFRInputFormLine> formlines = getMBCSFRInputFormLines(ctx, line.getBCS_FRInputForm_ID());
//		List<Nodes> nodeslines = getNodesLines(ctx, line.getBCS_FRInputForm_ID());
//		
//		for(Nodes  node : nodeslines){
//			BigDecimal sum = new BigDecimal(0); 
//			if(node.getParent_id() > 0){
//				int n = node.getParent_id(); 
//				int m = node.getNode_id();
//				
//				for(Nodes nodeInner: nodeslines){
//					int n2 = nodeInner.getParent_id();
//					int m3 = nodeInner.getNode_id();
//					
//					if(node.getNode_id() == nodeInner.getParent_id()){
//						for(int i = 0; i < formlines.size(); i++)
//							if(nodeInner.getNode_id() == formlines.get(i).getBCS_FRPositionsDict_ID()){
//								BigDecimal columnValue = formlines.get(i).getColumnValue1();
//								if(columnValue != null)
//									sum = sum.add(columnValue);
//							}
//							
//					}
//				}				
//			}
//			if(sum.compareTo(new BigDecimal(0)) == 1){
//				sql = "update BCS_FRInputFormLine set ColumnValue1 = " + sum + " where  BCS_FRInputForm_ID = " + line.getBCS_FRInputForm_ID() +" and BCS_FRPositionsDict_ID = " + node.getNode_id();
//				DB.executeUpdate(sql);
//				
//			}
//		}
//
//		mTab.dataRefreshAll(true);
//		mTab.getRowCount();
//		mTab.getCurrentRow();
//		
//		
//		return result;
//	}
	
//	public String calcColumnValue2(Properties ctx, int WindowNo, GridTab mTab, GridField mField, Object value) {
//		
//		int ID = mTab.getRecord_ID();
//		String sql = "update BCS_FRInputFormLine set ColumnValue2 = " + value + " where  BCS_FRInputFormLine_ID = " + ID;
//		DB.executeUpdate(sql);
//		
//		String result = "";
//		 
//		MBCSFRInputFormLine line = new MBCSFRInputFormLine(ctx, ID, null);
//
//		List<MBCSFRInputFormLine> formlines = getMBCSFRInputFormLines(ctx, line.getBCS_FRInputForm_ID());
//		List<Nodes> nodeslines = getNodesLines(ctx, line.getBCS_FRInputForm_ID());
//		
//		for(Nodes  node : nodeslines){
//			BigDecimal sum = new BigDecimal(0); 
//			if(node.getParent_id() > 0){
//				int n = node.getParent_id(); 
//				int m = node.getNode_id();
//				
//				for(Nodes nodeInner: nodeslines){
//					int n2 = nodeInner.getParent_id();
//					int m3 = nodeInner.getNode_id();
//					
//					if(node.getNode_id() == nodeInner.getParent_id()){
//						for(int i = 0; i < formlines.size(); i++)
//							if(nodeInner.getNode_id() == formlines.get(i).getBCS_FRPositionsDict_ID()){
//								BigDecimal columnValue = formlines.get(i).getColumnValue2();
//								if(columnValue != null)
//									sum = sum.add(columnValue);
//							}
//							
//					}
//				}				
//			}
//			if(sum.compareTo(new BigDecimal(0)) == 1){
//				sql = "update BCS_FRInputFormLine set ColumnValue2 = " + sum + " where  BCS_FRInputForm_ID = " + line.getBCS_FRInputForm_ID() +" and BCS_FRPositionsDict_ID = " + node.getNode_id();
//				DB.executeUpdate(sql);
//				
//			}
//		}
//
//		mTab.dataRefreshAll(true);
//		mTab.getRowCount();
//		mTab.getCurrentRow();
//		
//		
//		return result;
//	}

	
	
	protected List<Nodes> getNodesLines(Properties ctx, int BCS_FRInputForm_ID){
		List<Nodes> list = new ArrayList<Nodes>();
		
			String sql = 
					"SELECT n.node_id, n.parent_id \n"	//, l.BCS_FRInputFormLine_ID, d.BCS_FRPositionsDict_ID, d.name, d.isSummary, n.parent_ID \n"  
				+	"FROM adempiere.BCS_FRInputFormLine l \n"
				+	"inner join adempiere.BCS_FRPositionsDict d on d.BCS_FRPositionsDict_ID = l.BCS_FRPositionsDict_ID	\n"
				+	"inner join adempiere.Ad_TreeNodeBCS n on n.node_id = d.BCS_FRPositionsDict_ID	\n"
				+	"WHERE l.BCS_FRInputForm_ID = " + BCS_FRInputForm_ID;
		
			try {
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			
			while(rs.next()) {
					Nodes l = new Nodes(rs.getInt(1), rs.getInt(2));
					list.add(l);
			}
		} catch (Exception e) {
			s_log.log(Level.SEVERE, "Callout:	BCS_FRInputFormLine", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}			
		return list;
	}

	protected List<MBCSFRInputFormLine> getMBCSFRInputFormLines(Properties ctx, int MBCSFRInputForm){
		List<MBCSFRInputFormLine> list = new ArrayList<MBCSFRInputFormLine>();
		
			String sql = "SELECT BCS_FRInputFormLine_ID FROM BCS_FRInputFormLine WHERE BCS_FRInputForm_ID = " + MBCSFRInputForm;
		
			try {
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			
			while(rs.next()) {
					MBCSFRInputFormLine l = new MBCSFRInputFormLine(ctx, rs.getInt(1), null);
					list.add(l);
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "Callout:	BCS_FRInputFormLine", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}			
		return list;
	}
	
	class Nodes{
		int node_id;
		int parent_id;
		
		public Nodes(int node_id, int parent_id){
			this.node_id = node_id;
			this.parent_id = parent_id;
			
		}
		
		public int getNode_id() {
			return node_id;
		}
		public void setNode_id(int node_id) {
			this.node_id = node_id;
		}
		public int getParent_id() {
			return parent_id;
		}
		public void setParent_id(int parent_id) {
			this.parent_id = parent_id;
		}
		
		
	}
	
	
}
